#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/gpio.h>
#include <linux/ide.h>
#include <linux/types.h> 
#define LED_MAJOR	200				/* 主设备号 */
#define LED_NAME		"led" 	/* 设备名     */

#define LEDOFF 	0				/* 关灯 */
#define LEDON 	1				/* 开灯 */
/*
	pa_ccm_ccgrx  =   0x020C406C,
	pa_iomuxc_mux =   0x020E006C,
	pa_iomux_pad  =   0x020E02F8,
	pa_dr         =   0x0209C000
	pa_gdir       =   0x0209C004



*/

/* 寄存器物理地址 */
//GPIO1_IO04
#define CCM_CCGR1_BASE				(0X020C406C)	
#define SW_MUX_GPIO1_IO04_BASE		(0x020E006C)
#define SW_PAD_GPIO1_IO04_BASE		(0x020E02F8)
#define GPIO1_DR_BASE				(0x0209C000)
#define GPIO1_GDIR_BASE				(0x0209C004)

/* 映射后的寄存器虚拟地址指针 */
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO04;
static void __iomem *SW_PAD_GPIO1_IO04;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

/*
 * @description		: LED打开/关闭
 * @param - sta 	: LEDON(0) 打开LED，LEDOFF(1) 关闭LED
 * @return 			: 无
 */
void led_switch(u8 sta)
{
	u32 val = 0;
	if(sta == LEDON) {
		val = readl(GPIO1_DR);
		val &= ~(1 << 4);	
		writel(val, GPIO1_DR);
	}else if(sta == LEDOFF) {
		val = readl(GPIO1_DR);
		val|= (1 << 4);	
		writel(val, GPIO1_DR);
	}	
}

static int led_open(struct inode *inode, struct file *filp)
{
	printk("drive:led open!\r\n");
	return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;    
    retvalue = copy_from_user(databuf, buf, cnt);
    	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0];		/* 获取状态值 */

	if(ledstat == LEDON) {	
		printk("driver:LED_ON\r\n");
		led_switch(LEDON);		/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		led_switch(LEDOFF);	/* 关闭LED灯 */
	}
	//printk("drive:chrdevbase write!\r\n");
	return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
	printk("drive:led release!\r\n");
	return 0;
}

static struct file_operations led_fops = {
	.owner = THIS_MODULE,	
	.open = led_open,
	.write = led_write,
	.release = led_release,
};
static int __init led_init(void)
{
    int retvalue = 0;
    u32 val = 0;
	/* 初始化LED */
	/* 1、寄存器地址映射 */
  	IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
	SW_MUX_GPIO1_IO04 = ioremap(SW_MUX_GPIO1_IO04_BASE, 4);
  	SW_PAD_GPIO1_IO04 = ioremap(SW_PAD_GPIO1_IO04_BASE, 4);
	GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
	GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);
	/* 2、使能GPIO1时钟 */
	val = readl(IMX6U_CCM_CCGR1);
	val &= ~(3 << 26);	/* 清楚以前的设置 */
	val |= (3 << 26);	/* 设置新值 */
	writel(val, IMX6U_CCM_CCGR1);
	/* 3、设置GPIO1_IO04的复用功能，将其复用为
	 *    GPIO1_IO04，最后设置IO属性。
	 */
	writel(5, SW_MUX_GPIO1_IO04);
	/*寄存器SW_PAD_GPIO1_IO03设置IO属性
	 *bit 16:0 HYS关闭
	 *bit [15:14]: 00 默认下拉
     *bit [13]: 0 kepper功能
     *bit [12]: 1 pull/keeper使能
     *bit [11]: 0 关闭开路输出
     *bit [7:6]: 10 速度100Mhz
     *bit [5:3]: 110 R0/6驱动能力
     *bit [0]: 0 低转换率
	 */
	writel(0x10B0, SW_PAD_GPIO1_IO04);
	//writel(0x1F838, SW_PAD_GPIO1_IO04);
	/* 4、设置GPIO1_IO04为输出功能 */
	val = readl(GPIO1_GDIR);
	val &= ~(1 << 4);	/* 清除以前的设置 */
	val |= (1 << 4);	/* 设置为输出 */
	writel(val, GPIO1_GDIR);
	/* 5、默认关闭LED */
	val = readl(GPIO1_DR);
	val |= (1 << 4);	
	writel(val, GPIO1_DR);

	/* 6、注册字符设备驱动 */

	/* 注册字符设备驱动 */
	retvalue = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
	if(retvalue < 0){
	printk("led driver register failed\r\n");
	}
	printk("led init!\r\n");
	return 0;

}


static void __exit led_exit(void)
{
	/* 取消映射 */
	iounmap(IMX6U_CCM_CCGR1);
	iounmap(SW_MUX_GPIO1_IO04);
	iounmap(SW_PAD_GPIO1_IO04);
	iounmap(GPIO1_DR);
	iounmap(GPIO1_GDIR);

	/* 注销字符设备驱动 */
	unregister_chrdev(LED_MAJOR, LED_NAME);
}
module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("jiayi");







